// index.js
const { ganntdata, colorMap, colorList, weekList } = require("./mock");
const { sumTimetoHour } = require('../../utils/calender');
// 获取应用实例
var dateBoxDom = null;
var ganntDom = null;

const app = getApp()
Page({
  data: {
    CELLWIDTH: 100,
    CELL_HEIGHT: 50,
    B_CELL_HEIGHT: 60,
    searchValue: '',
    visible: false,
    num: 70,
    left: 0,
    // 甘特图开始日期和结束日期
    startDate: '',
    startDateStr: '',
    endDate: '',
    endDateStr: '',
    defaultDate: [],
    shopName: '',
    shopNameList: [],
    shopNameVisible: false,
    carType: '',
    carTypeList: [],
    carTypeVisible: false,
    carState: '',
    carStateList: [],
    carStateVisible: false,
    isScroll: false,
    hasUserInfo: false,
    ganntdata: [],
    pre: 0
  },
  handleCalendar() {
    this.setData({ visible: true });
  },
  onJump(e) {
    console.log(e)
    var carid = e.currentTarget.dataset.carid;
    wx.navigateTo({
      url: '../date/date?carid=' + carid,
    })
  },
  handleConfirm(e) {
    this.setData({
      startDate: e.detail.value[0],
      endDate: e.detail.value[1],
      startDateStr: new Date(e.detail.value[0]).format('yyyy-MM-dd'),
      endDateStr: new Date(e.detail.value[1]).format('yyyy-MM-dd'),
    })
    this.initDateList();
    this.initGanntData();
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    var startDate = new Date(new Date().toLocaleDateString()).getTime();
    var endDate = startDate + 1000 * 60 * 60 * 24 * 30;
    var startDateStr = new Date(startDate).format('yyyy-MM-dd');
    var endDateStr = new Date(endDate).format('yyyy-MM-dd');
    var shopNameList = [
      {label: '夫君租车', value: '1', }
    ]
    var carTypeList = [
      {label: '全部', value: '', }
    ]
    var carStateList = [
      {label: '上架车辆', value: true},
      {label: '下架车辆', value: false},
    ]
    this.setData({
      startDate,
      startDateStr,
      endDate,
      endDateStr,
      defaultDate: [startDate, endDate],
      shopNameList,
      shopName: shopNameList[0],
      carStateList,
      carState: carStateList[0],
      carTypeList,
      carType: carTypeList[0],
      colorMap,
      colorList
    })
    this.initGanntData();
    this.initDateList();
  },
  initGanntData() {
    console.log(this.data.startDate)
    for (let i = 0; i < 20; i++) {
      ganntdata.push(ganntdata[0])      
   }
   ganntdata.forEach(item => {
    item.childs.forEach(car => {
      car.useDate.forEach(date => {
        date.leftHours = sumTimetoHour(new Date(date.startDate).getTime() - this.data.startDate);
        date.widthHours = sumTimetoHour(new Date(date.endDate).getTime() - new Date(date.startDate).getTime());
      })
    })
   })
   this.setData({
    ganntdata: ganntdata
   })
  },
  initDateList() {
    var startDate = this.data.startDate;
    var endDate = this.data.endDate;
    var dateList = [];
    do {
      dateList.push({date: new Date(startDate).format('MM-dd'),times: new Date(startDate).getTime(), week: weekList[new Date(startDate).getDay()]});
      startDate += 1000 * 60 * 60 * 24;
    } while (endDate >= startDate);
    this.setData({
      dateList
    })
  },
  onReady() {
    wx.createSelectorQuery()
      .select('#date-box')
      .node()
      .exec((res) => {
        console.log(res)
        const scrollView = res[0].node;
        dateBoxDom = scrollView;
      })
      wx.createSelectorQuery()
      .select('#gannt-container')
      .node()
      .exec((res) => {
        console.log(res)
        const scrollView = res[0].node;
        ganntDom = scrollView;
      })

  },
  // 搜索事件
  onSearch() {
    console.log("搜索框的值为: " + this.data.searchValue)
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  onScroll(e) {
    console.log(e)
    dateBoxDom.scrollTo({left: e.detail.scrollLeft})
    this.setData({
      left: e.detail.scrollLeft
    })
  },
  throttle (fn, delay = 2000) {
    let pre = this.data.pre // 初始值是 0
    let that = this
    return function () {
        let now = + new Date();
        if(now - pre >= delay) {
            fn.apply(this, arguments)
            that.setData({
                pre: now
            })
        }
    }
},
  onScroll2(e) {

  },
  binddragstart() {
    this.setData({
      isScroll: true
    })
  },
  binddragend() {
    this.setData({
      isScroll: false
    })
  },
  onOpenDialog(e) {
    const { key } = e.currentTarget.dataset;
    this.setData({
      [`${key}Visible`]: true,
    });
  },
  onColumnChange(e) {
    console.log('picker pick:', e);
  },

  onPickerChange(e) {
    const { key } = e.currentTarget.dataset;
    const { value } = e.detail;
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: value.join(' '),
    });
  },

})